package Gui;

import java.io.File;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import Web.RunWebThread;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
* Description :select an algorithm
* <p>
*
*@author Danielle
*/
public class SelectAlgo extends org.eclipse.swt.widgets.Composite {
	private Composite composite1;
	private Button[] select;
	private String[] stringOfFiles;
	private static String[] filesSelcted;
	private static File direct;
	private Button button1;
	private Button cancel;
	private Button OK;
	static Shell shell;

	
	/**
	* Overriding checkSubclass allows this class to extend org.eclipse.swt.widgets.Composite
	*/	
	protected void checkSubclass() {
	}
	
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	
	public static String[] showGUI(File dir) {
		direct=dir;
		Display display = Display.getDefault();
		shell = new Shell(display);
		SelectAlgo inst = new SelectAlgo(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		
		return filesSelcted;
	}
	
	public SelectAlgo(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}

	private void initGUI() {
		try {
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			{
				String[] list = (direct).list(new myFilter());
				final int size=list.length;
				//size=4;////////////<----------------------
				
				composite1 = new Composite(this, SWT.NONE);
				FormLayout composite1Layout = new FormLayout();
				FormData composite1LData = new FormData();
				composite1LData.left =  new FormAttachment(0, 1000, 0);
				composite1LData.top =  new FormAttachment(0, 1000, 0);
				composite1LData.width = 432;
				composite1LData.height = 297;
				composite1.setLayoutData(composite1LData);
				composite1.setLayout(composite1Layout);
				

				{
					cancel = new Button(composite1, SWT.PUSH | SWT.CENTER);
					FormData cancelLData = new FormData();
					cancelLData.left =  new FormAttachment(0, 1000, 348);
					cancelLData.top =  new FormAttachment(0, 1000, 262);
					cancelLData.width = 52;
					cancelLData.height = 23;
					cancel.setLayoutData(cancelLData);
					cancel.setText("cancel");
					cancel.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							//System.out.println("cancel.widgetSelected, event="+evt);
							filesSelcted=null;
							shell.dispose();
							//TODO add your code for cancel.widgetSelected
						}
					});
				}
				{
					OK = new Button(composite1, SWT.PUSH | SWT.CENTER);
					FormData OKLData = new FormData();
					OKLData.left =  new FormAttachment(658, 1000, 0);
					OKLData.top =  new FormAttachment(883, 1000, 0);
					OKLData.width = 43;
					OKLData.height = 22;
					
					
					OK.setLayoutData(OKLData);
					OK.setText("OK");
					OK.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							//System.out.println("OK.widgetSelected, event="+evt);
							int counter=0;
							int i;
							for(i=0; i<size; i++)
							{
								if(select[i].getSelection())
								{
									counter++;
								}
							}
							int numOfFiles=counter;
							filesSelcted=new String[numOfFiles];
							counter=0;
							for(i=0; i<size; i++)
							{
								if(select[i].getSelection())
								{
									filesSelcted[counter]=select[i].getText();
									counter++;
								}
							}
							shell.dispose();
							//TODO add your code for OK.widgetSelected
						}
					});
				}
				
				
				int i;
				String str;
				select=new Button[size];
				
				for(i=0; i<size; i++)
				{
					FormData layout=new FormData();
					layout.left =  new FormAttachment(20, 1000, 0);
					layout.top =  new FormAttachment(50, 1000, 0);
					layout.width = 300;
					layout.height = 23;
					layout.left =  new FormAttachment(20, 1000, 0);
					layout.top =  new FormAttachment(50+70*i, 1000, 0);
					
					select[i] = new Button(composite1, SWT.CHECK | SWT.LEFT);
					select[i].setLayoutData(layout);
					str= "" +list[i];
					select[i].setText(str);
					select[i].setSelection(true);
					
				}
				
				
			}
			this.layout();
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
